<?php
namespace Swiftx\View;
use Swiftx\Interfaces\Http\Response;
use Swiftx\Interfaces\Ioc\Container;
use Swiftx\Interfaces\Router\Dispatcher;
use Swiftx\Interfaces\View\Engine;

class Prototype implements Response {

    /**
     * 注入容器
     * @var Container
     */
    protected $container;

    /**
     * 注入路由
     * @var Dispatcher
     */
    protected $router;

    /**
     * 模板引擎
     * @var Engine
     */
    protected $engine;

    /**
     * 模板文件
     * @var string
     */
    protected $template;

    /**
     * 构造函数
     * @param Container $container = provider(Application)
     * @param Dispatcher $dispatcher = provider(Router)
     */
    public function __construct(Container $container, Dispatcher $dispatcher){
        $this->container = $container;
        $this->router = $dispatcher;
    }

    /**
     * 设置引擎
     * @param string $value
     */
    public function setEngine(string $value){
        $this->engine = $this->container->fetch($value);
    }

    /**
     * 设置模板
     * @param string $value
     */
    public function setTemplate(string $value){
        $this->template = $value;
    }

    /**
     * 发送数据
     * @return void
     */
    public function send(){
        $class = new \ReflectionClass($this);
        foreach($class->getMethods(\ReflectionMethod::IS_PUBLIC) as $method){
            $doc = $method->getDocComment();
            $params = array();
            if(!preg_match('/@assign( )+(.*)/',$doc, $params)) continue;
            $this->engine->assign(trim($params[2]), $method->invoke($this));
        }
        $this->engine->display($this->template);
    }

}